Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 737)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.02734 13.02385 13.02042 13.01703 13.01370 13.01041 13.00716 13.00395
## [9] 13.00079 12.99765 12.99455 12.99149 12.98845 12.98543 12.98244 12.97947
## [17] 12.97651 12.97357 12.97065 12.96773 12.96483 12.96193 12.95903 12.95613
## [25] 12.95323 12.95032 12.94741 12.94449 12.94155 12.93860 12.93564 12.93265
## [33] 12.92964 12.92661 12.92355 12.92046 12.91734 12.91418 12.91100 12.90780
## [41] 12.90458 12.90135 12.89810 12.89485 12.89160 12.88834 12.88509 12.88185
## [49] 12.87862 12.87541 12.87222 12.86906 12.86592 12.86281 12.85974 12.85671
## [57] 12.85373 12.85079 12.84790 12.84507 12.84230 12.83959 12.83695 12.83437
## [65] 12.83188 12.82946 12.82713 12.82488 12.82272 12.82066 12.81869 12.81683
## [73] 12.81507 12.81342 12.81189 12.81047 12.80918 12.80801 12.80679 12.80536
## [81] 12.80372 12.80189 12.79989 12.79773 12.79542 12.79299 12.79043 12.78777
## [89] 12.78501 12.78219 12.77930 12.77636 12.77339 12.77040 12.76741 12.76442
## [97] 12.76146 12.75854 12.75566 12.75285 12.75013 12.74749 12.74496 12.74256
## [105] 12.74029 12.73817 12.73621 12.73443 12.73285 12.73147 12.73031 12.72939
## [113] 12.72871 12.72830 12.72817 12.72832 12.72879 12.72957 12.73068 12.73214
## [121] 12.73423 12.73717 12.74090 12.74537 12.75053 12.75631 12.76266 12.76952
## [129] 12.77683 12.78455 12.79261 12.80096 12.80953 12.81828 12.82714 12.83606
## [137] 12.84499 12.85386 12.86262 12.87122 12.87959 12.88768 12.89544 12.90280
## [145] 12.90971 12.91612 12.92196 12.92877 12.93794 12.94924 12.96240 12.97718
## [153] 12.99333 13.01061 13.02876 13.04753 13.06667 13.08595 13.10509 13.12386
## [161] 13.14202 13.15930 13.17545 13.19024 13.20341 13.21471 13.22390 13.23072
## [169] 13.23673 13.24359 13.25122 13.25953 13.26843 13.27783 13.28764 13.29776
## [177] 13.30812 13.31862 13.32917 13.33968 13.35007 13.36024 13.37011 13.37958
## [185] 13.38857 13.39698 13.40473 13.41173 13.41789 13.42312 13.42733 13.43043
## [193] 13.43233 13.43295 13.43219 13.42996 13.42617 13.42074 13.41350 13.40442
## [201] 13.39364 13.38130 13.36753 13.35247 13.33625 13.31900 13.30087 13.28199
## [209] 13.26249 13.24251 13.22219 13.20165 13.18104 13.16048 13.14012 13.12010
## [217] 13.10054 13.08157 13.06335 13.04338 13.01939 12.99183 12.96117 12.92786
## [225] 12.89234 12.85508 12.81652 12.77714 12.73736 12.69767 12.65850 12.62031
## [233] 12.58355 12.54869 12.51617 12.48646 12.45999 12.43724 12.41613 12.39436
## [241] 12.37205 12.34929 12.32619 12.30284 12.27936 12.25584 12.23239 12.20911
## [249] 12.18610 12.16347 12.14131 12.11974 12.09886 12.07876 12.05955 12.04134
## [257] 12.02422 12.00830 11.99368 11.98103 11.97080 11.96275 11.95667 11.95234
## [265] 11.94952 11.94800 11.94755 11.94795 11.94898 11.95041 11.95202 11.95358
## [273] 11.95487 11.95567 11.95575 11.95489 11.95287 11.94946 11.94444 11.93759
## [281] 11.93019 11.92361 11.91776 11.91256 11.90792 11.90374 11.89994 11.89643
## [289] 11.89312 11.88993 11.88675 11.88350 11.88010 11.87646 11.87248 11.86807
## [297] 11.86315 11.85763 11.85142 11.84443 11.83657 11.82777 11.81809 11.80768
## [305] 11.79664 11.78512 11.77324 11.76113 11.74891 11.73671 11.72466 11.71289
## [313] 11.70153 11.69069 11.68052 11.67113 11.66266 11.65318 11.64087 11.62609
## [321] 11.60914 11.59037 11.57009 11.54864 11.52634 11.50352 11.48052 11.45765
## [329] 11.43524 11.41363 11.39314 11.37410 11.35684 11.34169 11.32896 11.31900
## [337] 11.31213 11.30867 11.30700 11.30529 11.30364 11.30209 11.30074 11.29964
## [345] 11.29888 11.29852 11.29863 11.29929 11.30057 11.30254 11.30528 11.30885
## [353] 11.31333 11.31878 11.32529 11.33292 11.34174 11.35183 11.36325 11.37703
## [361] 11.39396 11.41382 11.43636 11.46137 11.48861 11.51785 11.54885 11.58139
## [369] 11.61523 11.65015 11.68590 11.72226 11.75900 11.79589 11.83269 11.86917
## [377] 11.90511 11.94026 11.97440 12.00730 12.03872 12.06844 12.09622 12.12183
## [385] 12.14504 12.16923 12.19754 12.22933 12.26398 12.30085 12.33931 12.37873
## [393] 12.41850 12.45796 12.49651 12.53350 12.56830 12.60030 12.62885 12.65333
## [401] 12.67311 12.69060 12.70854 12.72682 12.74532 12.76395 12.78257 12.80107
## [409] 12.81935 12.83729 12.85478 12.87169 12.88792 12.90336 12.91789 12.93140
## [417] 12.94376 12.95488 12.96464 12.97291 12.97960 12.98458 12.98714 12.98682
## [425] 12.98386 12.97852 12.97103 12.96166 12.95064 12.93823 12.92467 12.91021
## [433] 12.89511 12.87960 12.86394 12.84838 12.83316 12.81854 12.80476 12.79206
## [441] 12.78071 12.77094 12.76301 12.75470 12.74381 12.73061 12.71535 12.69830
## [449] 12.67972 12.65986 12.63898 12.61736 12.59525 12.57290 12.55059 12.52857
## [457] 12.50710 12.48644 12.46686 12.44861 12.43196 12.41717 12.40449 12.39419
## [465] 12.38446 12.37344 12.36131 12.34826 12.33445 12.32008 12.30533 12.29038
## [473] 12.27540 12.26059 12.24612 12.23217 12.21893 12.20658 12.19529 12.18526
## [481] 12.17665 12.16966 12.16447 12.16056 12.15729 12.15464 12.15259 12.15111
## [489] 12.15020 12.14983 12.14997 12.15061 12.15172 12.15330 12.15530 12.15773
## [497] 12.16055 12.16374 12.16729 12.17117 12.17536 12.17985 12.18461 12.18962
## [505] 12.19606 12.20497 12.21612 12.22929 12.24425 12.26076 12.27860 12.29755
## [513] 12.31737 12.33783 12.35872 12.37980 12.40084 12.42162 12.44190 12.46147
## [521] 12.48008 12.49752 12.51356 12.52796 12.54051 12.55096 12.55910 12.56672
## [529] 12.57568 12.58583 12.59704 12.60918 12.62210 12.63568 12.64978 12.66426
## [537] 12.67898 12.69381 12.70862 12.72326 12.73760 12.75151 12.76484 12.77747
## [545] 12.78925 12.80006 12.80974 12.81818 12.82523 12.83075 12.83461 12.83668
## [553] 12.83681 12.83488 12.83074 12.82441 12.81613 12.80605 12.79438 12.78127
## [561] 12.76690 12.75146 12.73511 12.71804 12.70043 12.68244 12.66425 12.64605
## [569] 12.62800 12.61029 12.59309 12.57657 12.56092 12.54631 12.53021 12.51023
## [577] 12.48678 12.46025 12.43106 12.39960 12.36628 12.33150 12.29565 12.25916
## [585] 12.22240 12.18580 12.14975 12.11465 12.08090 12.04892 12.01910 11.99184
## [593] 11.96754 11.94662 11.92946 11.91329 11.89520 11.87545 11.85428 11.83194
## [601] 11.80866 11.78471 11.76032 11.73574 11.71121 11.68698 11.66331 11.64042
## [609] 11.61858 11.59802 11.57899 11.56174 11.54651 11.53355 11.52311 11.51543
## [617] 11.51028 11.50716 11.50594 11.50646 11.50857 11.51214 11.51701 11.52303
## [625] 11.53007 11.53797 11.54658 11.55577 11.56537 11.57525 11.58526 11.59525
## [633] 11.60508 11.61460 11.62365 11.63210 11.63980 11.64870 11.66059 11.67509
## [641] 11.69183 11.71043 11.73052 11.75172 11.77365 11.79593 11.81819 11.84006
## [649] 11.86115 11.88109 11.89951 11.91602 11.93025 11.94363 11.95781 11.97270
## [657] 11.98825 12.00438 12.02103 12.03811 12.05558 12.07335 12.09135 12.10952
## [665] 12.12779 12.14609 12.16435 12.18250 12.20047 12.21819 12.23560 12.25261
## [673] 12.26917 12.28521 12.30065 12.31543 12.32947 12.34271 12.35508 12.36701
## [681] 12.37896 12.39091 12.40286 12.41480 12.42670 12.43855 12.45034 12.46206
## [689] 12.47370 12.48523 12.49665 12.50793 12.51908 12.53007 12.54090 12.55154
## [697] 12.56198 12.57221 12.58222 12.59199 12.60151 12.61085 12.62010 12.62924
## [705] 12.63827 12.64720 12.65601 12.66470 12.67327 12.68172 12.69003 12.69822
## [713] 12.70627 12.71418 12.72194 12.72956 12.73703 12.74434 12.75149 12.75848
## [721] 12.76530 12.77193 12.77833 12.78453 12.79052 12.79631 12.80192 12.80735
## [729] 12.81261 12.81770 12.82264 12.82744 12.83209 12.83662 12.84102 12.84531
## [737] 12.84950
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 737)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.63018 12.62539 12.62070 12.61611 12.61162 12.60723 12.60293 12.59873
## [9] 12.59461 12.59059 12.58665 12.58280 12.57904 12.57536 12.57176 12.56824
## [17] 12.56480 12.56144 12.55815 12.55493 12.55179 12.54872 12.54571 12.54277
## [25] 12.53990 12.53709 12.53434 12.53165 12.52902 12.52645 12.52393 12.52146
## [33] 12.51905 12.51669 12.51437 12.51210 12.50988 12.50768 12.50553 12.50342
## [41] 12.50135 12.49933 12.49736 12.49545 12.49359 12.49179 12.49006 12.48839
## [49] 12.48679 12.48526 12.48381 12.48243 12.48114 12.47993 12.47880 12.47777
## [57] 12.47683 12.47598 12.47523 12.47459 12.47405 12.47362 12.47330 12.47309
## [65] 12.47300 12.47303 12.47318 12.47346 12.47387 12.47440 12.47508 12.47589
## [73] 12.47684 12.47794 12.47918 12.48058 12.48212 12.48382 12.48566 12.48762
## [81] 12.48968 12.49186 12.49415 12.49654 12.49905 12.50166 12.50438 12.50720
## [89] 12.51012 12.51314 12.51627 12.51949 12.52281 12.52622 12.52973 12.53333
## [97] 12.53703 12.54081 12.54469 12.54865 12.55270 12.55683 12.56105 12.56535
## [105] 12.56973 12.57419 12.57873 12.58335 12.58804 12.59281 12.59765 12.60257
## [113] 12.60755 12.61260 12.61773 12.62291 12.62817 12.63348 12.63886 12.64431
## [121] 12.64998 12.65603 12.66243 12.66913 12.67611 12.68334 12.69077 12.69837
## [129] 12.70612 12.71396 12.72188 12.72984 12.73780 12.74573 12.75359 12.76135
## [137] 12.76898 12.77644 12.78369 12.79072 12.79746 12.80391 12.81163 12.82204
## [145] 12.83486 12.84983 12.86666 12.88508 12.90481 12.92558 12.94712 12.96914
## [153] 12.99137 13.01354 13.03537 13.05658 13.07690 13.09606 13.11377 13.12976
## [161] 13.14376 13.15549 13.16468 13.17287 13.18175 13.19124 13.20127 13.21177
## [169] 13.22265 13.23384 13.24528 13.25688 13.26856 13.28026 13.29190 13.30340
## [177] 13.31470 13.32571 13.33635 13.34656 13.35627 13.36538 13.37384 13.38156
## [185] 13.38848 13.39450 13.39957 13.40361 13.40653 13.40827 13.40876 13.40791
## [193] 13.40565 13.40191 13.39661 13.38967 13.38033 13.36803 13.35302 13.33558
## [201] 13.31596 13.29441 13.27121 13.24660 13.22086 13.19423 13.16699 13.13938
## [209] 13.11167 13.08412 13.05699 13.03054 13.00502 12.98071 12.95785 12.93672
## [217] 12.91756 12.89716 12.87252 12.84426 12.81296 12.77924 12.74371 12.70696
## [225] 12.66960 12.63224 12.59547 12.55991 12.52615 12.49481 12.46648 12.44178
## [233] 12.42130 12.40274 12.38348 12.36362 12.34328 12.32256 12.30158 12.28045
## [241] 12.25928 12.23818 12.21726 12.19663 12.17641 12.15670 12.13761 12.11926
## [249] 12.10176 12.08521 12.06973 12.05543 12.04243 12.03082 12.02129 12.01431
## [257] 12.00963 12.00703 12.00627 12.00713 12.00937 12.01277 12.01709 12.02210
## [265] 12.02758 12.03329 12.03899 12.04447 12.04949 12.05381 12.05721 12.05946
## [273] 12.06033 12.05958 12.05699 12.05435 12.05350 12.05424 12.05636 12.05966
## [281] 12.06395 12.06902 12.07468 12.08073 12.08695 12.09317 12.09917 12.10476
## [289] 12.10973 12.11388 12.11703 12.11896 12.11947 12.11837 12.11546 12.11054
## [297] 12.10393 12.09616 12.08736 12.07763 12.06709 12.05586 12.04405 12.03177
## [305] 12.01915 12.00629 11.99330 11.98031 11.96743 11.95478 11.94246 11.93059
## [313] 11.91929 11.90867 11.89885 11.88781 11.87367 11.85678 11.83746 11.81605
## [321] 11.79287 11.76826 11.74255 11.71607 11.68916 11.66214 11.63536 11.60913
## [329] 11.58379 11.55968 11.53713 11.51646 11.49802 11.48212 11.46911 11.45932
## [337] 11.45307 11.44842 11.44328 11.43777 11.43203 11.42618 11.42034 11.41464
## [345] 11.40920 11.40417 11.39965 11.39578 11.39268 11.39048 11.38930 11.38928
## [353] 11.39053 11.39319 11.39738 11.40322 11.41085 11.42103 11.43430 11.45040
## [361] 11.46907 11.49009 11.51318 11.53811 11.56463 11.59249 11.62143 11.65122
## [369] 11.68159 11.71231 11.74312 11.77377 11.80402 11.83361 11.86230 11.88984
## [377] 11.91598 11.94047 11.96306 11.98350 12.00457 12.02897 12.05629 12.08612
## [385] 12.11808 12.15175 12.18675 12.22266 12.25910 12.29566 12.33194 12.36753
## [393] 12.40206 12.43510 12.46626 12.49515 12.52135 12.54448 12.56414 12.58249
## [401] 12.60189 12.62217 12.64317 12.66472 12.68666 12.70883 12.73105 12.75318
## [409] 12.77505 12.79649 12.81733 12.83743 12.85660 12.87470 12.89155 12.90699
## [417] 12.92086 12.93299 12.94323 12.95140 12.95777 12.96273 12.96638 12.96878
## [425] 12.97001 12.97016 12.96930 12.96751 12.96487 12.96145 12.95733 12.95260
## [433] 12.94732 12.94158 12.93545 12.92902 12.92235 12.91554 12.90865 12.90176
## [441] 12.89495 12.88636 12.87427 12.85905 12.84104 12.82058 12.79802 12.77370
## [449] 12.74799 12.72121 12.69373 12.66588 12.63801 12.61047 12.58361 12.55777
## [457] 12.53330 12.51055 12.48987 12.47159 12.45607 12.44366 12.43182 12.41798
## [465] 12.40236 12.38523 12.36681 12.34737 12.32714 12.30637 12.28530 12.26419
## [473] 12.24327 12.22278 12.20299 12.18412 12.16643 12.15017 12.13556 12.12288
## [481] 12.11234 12.10421 12.09873 12.09479 12.09112 12.08777 12.08474 12.08207
## [489] 12.07978 12.07790 12.07644 12.07544 12.07492 12.07490 12.07540 12.07646
## [497] 12.07810 12.08034 12.08321 12.08672 12.09092 12.09581 12.10143 12.10779
## [505] 12.11628 12.12804 12.14279 12.16024 12.18010 12.20208 12.22590 12.25126
## [513] 12.27787 12.30545 12.33372 12.36237 12.39112 12.41969 12.44778 12.47510
## [521] 12.50137 12.52630 12.54960 12.57098 12.59016 12.60683 12.62072 12.63428
## [529] 12.65000 12.66768 12.68711 12.70809 12.73042 12.75388 12.77827 12.80339
## [537] 12.82902 12.85496 12.88101 12.90696 12.93260 12.95773 12.98214 13.00563
## [545] 13.02798 13.04900 13.06847 13.08619 13.10196 13.11556 13.12680 13.13546
## [553] 13.14134 13.14423 13.14394 13.14071 13.13509 13.12728 13.11747 13.10587
## [561] 13.09267 13.07808 13.06230 13.04552 13.02796 13.00979 12.99124 12.97248
## [569] 12.95374 12.93520 12.91707 12.89955 12.88283 12.86712 12.84983 12.82850
## [577] 12.80354 12.77536 12.74435 12.71092 12.67548 12.63842 12.60015 12.56108
## [585] 12.52161 12.48213 12.44306 12.40480 12.36775 12.33232 12.29890 12.26791
## [593] 12.23974 12.21480 12.19350 12.17250 12.14845 12.12172 12.09266 12.06163
## [601] 12.02900 11.99513 11.96037 11.92510 11.88966 11.85443 11.81976 11.78601
## [609] 11.75354 11.72272 11.69391 11.66747 11.64375 11.62312 11.60594 11.59258
## [617] 11.58175 11.57194 11.56310 11.55522 11.54827 11.54222 11.53705 11.53272
## [625] 11.52921 11.52650 11.52455 11.52334 11.52284 11.52302 11.52386 11.52534
## [633] 11.52741 11.53006 11.53326 11.53699 11.54120 11.54766 11.55781 11.57120
## [641] 11.58739 11.60594 11.62638 11.64828 11.67118 11.69464 11.71820 11.74142
## [649] 11.76385 11.78504 11.80454 11.82190 11.83668 11.85071 11.86604 11.88258
## [657] 11.90022 11.91885 11.93837 11.95868 11.97967 12.00123 12.02326 12.04566
## [665] 12.06832 12.09114 12.11401 12.13683 12.15949 12.18188 12.20391 12.22547
## [673] 12.24646 12.26676 12.28627 12.30490 12.32253 12.33906 12.35438 12.36919
## [681] 12.38423 12.39946 12.41486 12.43040 12.44604 12.46178 12.47756 12.49337
## [689] 12.50918 12.52496 12.54068 12.55630 12.57182 12.58718 12.60237 12.61736
## [697] 12.63212 12.64661 12.66082 12.67471 12.68826 12.70161 12.71494 12.72823
## [705] 12.74148 12.75468 12.76782 12.78089 12.79388 12.80679 12.81961 12.83232
## [713] 12.84493 12.85741 12.86977 12.88199 12.89407 12.90600 12.91777 12.92937
## [721] 12.94079 12.95198 12.96288 12.97351 12.98390 12.99406 13.00400 13.01374
## [729] 13.02330 13.03270 13.04196 13.05108 13.06010 13.06902 13.07786 13.08664
## [737] 13.09538
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 737)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.02680 12.02160 12.01650 12.01148 12.00654 12.00169 11.99692 11.99222
## [9] 11.98760 11.98305 11.97856 11.97415 11.96979 11.96550 11.96126 11.95708
## [17] 11.95295 11.94886 11.94483 11.94084 11.93689 11.93297 11.92910 11.92525
## [25] 11.92144 11.91765 11.91389 11.91015 11.90643 11.90272 11.89903 11.89535
## [33] 11.89167 11.88800 11.88434 11.88067 11.87700 11.87333 11.86965 11.86595
## [41] 11.86224 11.85852 11.85477 11.85100 11.84721 11.84339 11.83956 11.83573
## [49] 11.83189 11.82805 11.82423 11.82042 11.81664 11.81288 11.80916 11.80548
## [57] 11.80184 11.79826 11.79474 11.79128 11.78790 11.78458 11.78136 11.77822
## [65] 11.77518 11.77223 11.76940 11.76667 11.76407 11.76159 11.75925 11.75704
## [73] 11.75497 11.75305 11.75129 11.74969 11.74826 11.74700 11.74592 11.74502
## [81] 11.74432 11.74382 11.74351 11.74342 11.74355 11.74370 11.74372 11.74362
## [89] 11.74340 11.74310 11.74272 11.74228 11.74179 11.74128 11.74076 11.74024
## [97] 11.73975 11.73929 11.73889 11.73855 11.73831 11.73816 11.73813 11.73824
## [105] 11.73849 11.73892 11.73952 11.74033 11.74135 11.74260 11.74410 11.74586
## [113] 11.74791 11.75024 11.75289 11.75587 11.75919 11.76288 11.76693 11.77139
## [121] 11.77625 11.78153 11.78726 11.79344 11.80010 11.80725 11.81575 11.82633
## [129] 11.83880 11.85298 11.86867 11.88569 11.90385 11.92296 11.94283 11.96328
## [137] 11.98410 12.00512 12.02615 12.04700 12.06748 12.08739 12.10656 12.12480
## [145] 12.14191 12.15770 12.17199 12.18733 12.20614 12.22806 12.25272 12.27976
## [153] 12.30881 12.33950 12.37147 12.40436 12.43780 12.47142 12.50486 12.53775
## [161] 12.56973 12.60043 12.62949 12.65653 12.68120 12.70314 12.72196 12.73731
## [169] 12.75111 12.76546 12.78026 12.79544 12.81090 12.82657 12.84234 12.85815
## [177] 12.87389 12.88948 12.90484 12.91988 12.93451 12.94865 12.96220 12.97509
## [185] 12.98722 12.99851 13.00887 13.01822 13.02646 13.03352 13.03929 13.04371
## [193] 13.04668 13.04811 13.04792 13.04602 13.04232 13.03674 13.02837 13.01654
## [201] 13.00157 12.98376 12.96340 12.94080 12.91627 12.89010 12.86261 12.83409
## [209] 12.80484 12.77517 12.74538 12.71578 12.68666 12.65833 12.63109 12.60525
## [217] 12.58110 12.55896 12.53911 12.51836 12.49358 12.46525 12.43385 12.39983
## [225] 12.36368 12.32586 12.28685 12.24710 12.20711 12.16732 12.12823 12.09029
## [233] 12.05397 12.01976 11.98811 11.95950 11.93440 11.91328 11.89447 11.87597
## [241] 11.85780 11.83994 11.82239 11.80516 11.78823 11.77161 11.75529 11.73927
## [249] 11.72355 11.70812 11.69299 11.67815 11.66360 11.64933 11.63534 11.62164
## [257] 11.60822 11.59507 11.58219 11.57062 11.56126 11.55390 11.54833 11.54434
## [265] 11.54174 11.54030 11.53983 11.54012 11.54096 11.54215 11.54347 11.54473
## [273] 11.54571 11.54621 11.54602 11.54493 11.54274 11.53925 11.53423 11.52750
## [281] 11.52025 11.51380 11.50806 11.50295 11.49841 11.49434 11.49068 11.48733
## [289] 11.48424 11.48131 11.47847 11.47564 11.47275 11.46971 11.46644 11.46288
## [297] 11.45894 11.45454 11.44960 11.44406 11.43782 11.42999 11.41993 11.40797
## [305] 11.39441 11.37958 11.36380 11.34738 11.33064 11.31391 11.29749 11.28171
## [313] 11.26688 11.25333 11.24137 11.23132 11.22350 11.21550 11.20493 11.19210
## [321] 11.17730 11.16085 11.14305 11.12421 11.10464 11.08464 11.06452 11.04458
## [329] 11.02514 11.00650 10.98897 10.97286 10.95846 10.94609 10.93606 10.92867
## [337] 10.92423 10.92305 10.92437 10.92721 10.93146 10.93702 10.94382 10.95176
## [345] 10.96074 10.97067 10.98147 10.99304 11.00529 11.01812 11.03144 11.04517
## [353] 11.05921 11.07347 11.08786 11.10228 11.11664 11.13086 11.14484 11.15996
## [361] 11.17755 11.19742 11.21939 11.24327 11.26887 11.29601 11.32449 11.35413
## [369] 11.38475 11.41615 11.44815 11.48056 11.51320 11.54588 11.57840 11.61060
## [377] 11.64226 11.67322 11.70328 11.73226 11.75996 11.78621 11.81081 11.83358
## [385] 11.85432 11.87526 11.89847 11.92360 11.95028 11.97814 12.00682 12.03595
## [393] 12.06518 12.09414 12.12246 12.14978 12.17573 12.19995 12.22208 12.24175
## [401] 12.25860 12.27414 12.29009 12.30635 12.32282 12.33942 12.35606 12.37263
## [409] 12.38906 12.40524 12.42109 12.43651 12.45142 12.46571 12.47931 12.49211
## [417] 12.50402 12.51496 12.52483 12.53353 12.54099 12.54710 12.55160 12.55438
## [425] 12.55557 12.55532 12.55374 12.55097 12.54715 12.54241 12.53687 12.53068
## [433] 12.52397 12.51686 12.50949 12.50199 12.49450 12.48715 12.48007 12.47339
## [441] 12.46725 12.46178 12.45711 12.45187 12.44473 12.43587 12.42546 12.41370
## [449] 12.40075 12.38680 12.37203 12.35661 12.34074 12.32458 12.30833 12.29215
## [457] 12.27623 12.26075 12.24589 12.23184 12.21876 12.20683 12.19625 12.18719
## [465] 12.17807 12.16730 12.15510 12.14168 12.12725 12.11200 12.09616 12.07992
## [473] 12.06349 12.04709 12.03091 12.01517 12.00008 11.98583 11.97265 11.96073
## [481] 11.95028 11.94151 11.93463 11.92847 11.92178 11.91466 11.90722 11.89955
## [489] 11.89177 11.88397 11.87624 11.86871 11.86145 11.85459 11.84821 11.84243
## [497] 11.83734 11.83304 11.82964 11.82723 11.82593 11.82582 11.82702 11.82962
## [505] 11.83395 11.84017 11.84812 11.85765 11.86861 11.88083 11.89417 11.90846
## [513] 11.92357 11.93932 11.95557 11.97216 11.98894 12.00575 12.02243 12.03885
## [521] 12.05483 12.07022 12.08488 12.09864 12.11135 12.12285 12.13300 12.14353
## [529] 12.15619 12.17077 12.18711 12.20501 12.22429 12.24477 12.26627 12.28860
## [537] 12.31157 12.33501 12.35872 12.38254 12.40626 12.42972 12.45271 12.47507
## [545] 12.49661 12.51714 12.53648 12.55445 12.57086 12.58553 12.59828 12.60891
## [553] 12.61725 12.62312 12.62633 12.62768 12.62813 12.62773 12.62654 12.62461
## [561] 12.62199 12.61875 12.61492 12.61058 12.60577 12.60054 12.59495 12.58906
## [569] 12.58291 12.57657 12.57008 12.56351 12.55689 12.55030 12.54209 12.53079
## [577] 12.51670 12.50012 12.48134 12.46065 12.43836 12.41476 12.39014 12.36481
## [585] 12.33906 12.31318 12.28747 12.26224 12.23776 12.21435 12.19229 12.17189
## [593] 12.15344 12.13723 12.12356 12.10980 12.09330 12.07439 12.05336 12.03053
## [601] 12.00621 11.98071 11.95434 11.92741 11.90023 11.87311 11.84636 11.82028
## [609] 11.79520 11.77142 11.74926 11.72901 11.71099 11.69551 11.68289 11.67343
## [617] 11.66627 11.66032 11.65549 11.65172 11.64894 11.64707 11.64605 11.64580
## [625] 11.64626 11.64734 11.64899 11.65112 11.65367 11.65657 11.65975 11.66313
## [633] 11.66664 11.67022 11.67379 11.67727 11.68061 11.68525 11.69248 11.70198
## [641] 11.71342 11.72649 11.74084 11.75617 11.77214 11.78843 11.80473 11.82069
## [649] 11.83601 11.85036 11.86340 11.87483 11.88431 11.89312 11.90271 11.91300
## [657] 11.92394 11.93545 11.94746 11.95992 11.97275 11.98588 11.99925 12.01278
## [665] 12.02642 12.04010 12.05374 12.06728 12.08065 12.09379 12.10662 12.11909
## [673] 12.13112 12.14264 12.15359 12.16390 12.17351 12.18234 12.19033 12.19792
## [681] 12.20561 12.21338 12.22119 12.22903 12.23689 12.24473 12.25254 12.26030
## [689] 12.26800 12.27559 12.28308 12.29044 12.29764 12.30466 12.31150 12.31812
## [697] 12.32450 12.33063 12.33648 12.34204 12.34728 12.35232 12.35726 12.36211
## [705] 12.36687 12.37152 12.37605 12.38047 12.38477 12.38893 12.39296 12.39685
## [713] 12.40059 12.40417 12.40759 12.41085 12.41394 12.41685 12.41957 12.42210
## [721] 12.42444 12.42652 12.42833 12.42986 12.43113 12.43216 12.43295 12.43353
## [729] 12.43390 12.43409 12.43409 12.43393 12.43361 12.43316 12.43258 12.43189
## [737] 12.43110
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")